package com.rongshu.view.userInfo.presenter;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.rongshu.R;
import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.discovery.bean.IsAtUserBlackResponse;
import com.rongshu.view.userInfo.adapter.DirectChatAdapter;
import com.rongshu.view.userInfo.entity.DirectChat;
import com.rongshu.view.userInfo.entity.sixinClass;
import com.rongshu.view.userInfo.iview.DirectChatView;
import com.widget.toast.ToastUtils;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Func1;

public class DirectChatPresenter extends BasePresenter<DirectChatView> {

    public DirectChatPresenter(Context context) {
        super(context);
    }

    /**
     * 私信消息列
     */
    public void getReceiveMessage(final int send_userid, final int receive_userid, int pageIndex, int pageSize, String sign) {
        RetorfitService.getReceiveMessage(send_userid, receive_userid, pageIndex, pageSize, sign)
                .map(new Func1<DirectChat, DirectChat>() {
                    @Override
                    public DirectChat call(DirectChat chat) {
                        List<DirectChat.ResponseBean> responseBeanList = chat.getResponse();
                        if (responseBeanList != null) {
                            for (DirectChat.ResponseBean responseBean : responseBeanList) {
                                if (send_userid == responseBean.getSend_userid()) {
                                    responseBean.setType(DirectChatAdapter.ONESELF);
                                } else {
                                    responseBean.setType(DirectChatAdapter.OTHERS);
                                }
                            }
                            Collections.reverse(responseBeanList);
                        }
                        return chat;
                    }
                })
                .subscribe(new Subscriber<DirectChat>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(DirectChat chat) {
                        if (getView() != null) {
                            getView().onDirectChatMsg(chat);
                        }
                    }
                });
    }

    /**
     * 发送私信消息
     */
    public void postSendMessage(int send_userid, int receive_userid, String sign, sixinClass sixinClass) {
        RetorfitService.postSendMessage(send_userid, receive_userid, sign, sixinClass)
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean iNetBean) {
                        if (getView() != null) {
                            getView().onDirectChatSendMsg(iNetBean);
                        }
                    }
                });
    }

    /**
     * 加入,移除黑名单
     */
    public void blackListOption(int user_id, int user_id_to, String sign) {
        RetorfitService.blackListOption(user_id, user_id_to, sign)
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Object o) {
                        if (getView() != null) {
                            INetBean iNetBean = new Gson().fromJson(o.toString(), INetBean.class);
                            getView().onUserAddBlack(iNetBean);
                        }
                    }
                });
    }

    /**
     * 判断用户是否被禁用
     */
    public void isAtUserDisabled(int user_id, int user_id_to, String sign) {
        RetorfitService.isAtUserBlack(user_id, user_id_to, sign)
                .compose(getView().<IsAtUserBlackResponse>bindToLife())
                .subscribe(new Subscriber<IsAtUserBlackResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(IsAtUserBlackResponse response) {
                        if (response.getErrCode() == 1) {
                            if (response.getResponse().getType() == 1 || response.getErrMsg().equals("该用户已经被封停")) {
                                ToastUtils.show(response.getErrMsg());
                            }
                        }
                    }
                });
    }
}
